import hashlib
import random


def md5_encode(original_str):
    """
    md5加密
    :param original_str:
    :return:
    """
    m = hashlib.md5()
    m.update(original_str.encode(encoding='UTF-8'))
    return m.hexdigest()

def _make_token(uid):
    MY = '5gzhike'
    all_token = str(md5_encode(f"{uid}{MY}"))
    b = random.randint(5,15)
    n = random.randint(5,10)
    token = f"B{b}{all_token[b:b+n]}N{n}"
    return token


def _check_token(uid,token):
    """
    B1#a8490c#N6
    """
    MY = '5gzhike'
    all_token = str(md5_encode(f"{uid}{MY}"))
    try:
        begin = token[:2]
        end = token[-2:]
        if len(token) != int(end[-1]) + 4:
            return False
        b,n = int(begin[-1]),int(end[-1])
        t = token[2:2+n]
    except:
        return False
    if all_token[b:b+n] == t:
        return True
    return False